﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using cludg.Domain;

namespace cludg.Repository.NHibernate.Utils
{
	public static class DictionaryPropertyHelper
	{
		public static PropertyPart MapEx<T>(this ClasslikeMapBase<T> mapBase, Type valueType, string key) where T: IExtendable
		{
			return mapBase.Map(x => x[key])
				.Access.Using<NHibernate.Utils.DictionaryPropertyAccessor>()
				.CustomType(valueType);
		}

		public static PropertyPart MapEx<T, TValue>(this ClasslikeMapBase<T> mapBase, string key) where T : IExtendable
		{
			return MapEx<T>(mapBase, typeof(TValue), key);
		}

		public static ManyToOnePart<object> ReferencesEx<T, TOther>(this ClasslikeMapBase<T> mapBase, string key) where T : IExtendable
		{
			return mapBase.References(x => x[key])
				.Access.Using<NHibernate.Utils.DictionaryPropertyAccessor>()
				.Class<TOther>();
		}
	}
}
